<template>
  <div class="page-article" v-if="model">
    <!-- 头部 -->
    <div class="d-flex px-2 py-3 article-herder">
      <div class="iconfont icon-back color" @click="out"></div>
      <strong class="d-flex flex-1 fs-md mx-2 text-ellipsis color">{{ model.title }}</strong>
      <div class="text-grey fs-xs">2020-03-02</div>
    </div>
    <!-- 详情 -->
    <div v-html="model.body" class="px-3 article-body p fs-xl"></div>
    <!-- 相关资讯 -->
    <div class="px-2 pb-3">
      <i class="iconfont icon-xiangguanlianjie px-2"></i>
      <strong class="fs-lg color">相关资讯</strong>
      <div class="pt-3 px-2 d-flex flex-column">
        <router-link tag="div" class="d-flex jc-between fs-lg py-1"
        :to="`/articles/${item._id}`" v-for="item in model.related" :key="item._id"
        >
          <span class="text-ellipsis" style="width: 60%">{{item.title}}</span>
          <div>{{item.createdAt | date}}</div>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
export default {
  props: {
    id: { required: true },
  },
  filters: {
    date(val) {
      return dayjs(val).format('YYYY-MM-DD')
    }
  },
  data() {
    return {
      model: {}
    }
  },
  methods: {
    async fetch() {
      const res = await this.$http.get(`/articles/${this.id}`)
      this.model = res.data
    },
    out() {
      this.$router.go(-1)
    }
  },
  created() {
    this.fetch()
  },
  watch: {
    id() {
      this.fetch()
    }
  }
}
</script>

<style lang="scss">
.page-article {
  .article-herder {
    border-bottom: 1px solid #d4d9de;
  }
  .color {
    color: #4394e4;
  }
  .article-body {
    img {
      max-width: 100%;
      height: auto;
    }
  }
}
</style>